package com.kdtech.analyse.video;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;

public class _163VideoAnalyse implements AnalyseNews {

	public static HashMap<String,String> FormatUrl(HashMap<String,String> list){
		if(list!=null && list.size()>0){
			HashMap<String,String> newList = new HashMap<String,String>();
			Iterator<Entry<String, String>> iterator = list.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry<String, String> next = iterator.next();
				String string = next.getKey();
				String value = next.getValue();
				if(string.startsWith("http://v.163.com/")){
					if(string.contains("#from=")){
						string  = StringUtils.substringBefore(string, "#from=");
					}
				}
				newList.put(string, value);
			}
			return newList;
		}else{
			return list;
		}
	}
	
	public boolean isDetailPage(String url) {
		if(url.contains("#from=")){
			url  = StringUtils.substringBefore(url, "#from=");
		}
		boolean bRet = false;
		//http://news.3xgd.com/html/201211/27/85282.html
		String[] regex = { 
				"http://v.163.com/[a-z]*/[A-Z0-9]+/[A-Z0-9]+.html" ,
				"http://v.auto.163.com/video/[0-9]+/.*/[A-Z0-9]+.html",
				
				
		};
		
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		video.setUrl(url);
		String title = null;
		Long date = null;
		String desc = null;
		String author=null;
		Document doc = Jsoup.parse(html);
		/*
		 * 解析标题
		 */
		title= doc.select("span#js_Set").text();
		/*
		 * 解析时间
		 */
		date = DateUtils.matchDate(doc.select("div.cDGray").text());
        
        desc = doc.select("div.video-digest p").text();
		
       Elements select = doc.select("div.cDGray span.item");
       if(select!=null && select.size()==2){
			author =StringUtils.substringAfter(select.get(0).text(), "来源：");
		}
       if (StringUtils.isBlank(title)||date==null) {
		}
		video.setTitle(title);
		video.setAuthor(author);
		video.setDate(date);
		video.setContent(desc);
		return video;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		_163VideoAnalyse a = new _163VideoAnalyse();
		String url = "http://v.163.com/zixun/V66934FOH/VAS3C0MUF.html";
		if (a.isDetailPage(url)) {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不匹配规则");
		}
	}
	

}
